"""
思路:
	1.源于链表结构
	2.封装栈的操作方法(入栈,出栈,栈空,栈顶元素)
	3.链表的开头作为栈顶
"""
# 自定义异常
class StackError(Exception):
	pass

# 节点
class Node:
	def __init__(self, value, next_=None):
		self.value = value
		self.next_ = next_

# 链式栈操作
class LStack:
	def __init__(self):
		self._top = None  # 标记栈顶

	def is_empty(self):
		"""判断是否为空"""
		return self._top is None

	def push(self, value):
		"""入栈"""
		self._top = Node(value, self._top)   # 让节点直接指向栈顶,而不用下面两步操作
		# node.next_ = self._top # 指向当前的head
		# self._top = node    # 吧当前节点移动到后一位入栈的元素上

	# 出栈
	def pop(self):
		"""出栈"""
		if self._top is None:
			raise StackError("栈为空.")

		value = self._top.value
		self._top = self._top.next_
		return value

	# 查看栈顶
	def top(self):
		"""查看栈顶"""
		if self._top is None:
			raise StackError("栈为空.")

		return self._top.value

if __name__ == '__main__':
	ls = LStack()
	ls.push(1)
	ls.push(2)
	ls.push(3)
	print(ls.pop())
